home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TEXT_TOG.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  10.8 KB  |  373 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.output.*;
  5. import sub_arctic.input.*;
  6. import sub_arctic.constraints.std_function;
  7.  
  8. import java.awt.Font;
  9. import java.util.Vector;
  10.  
  11. /**
  12.  * This object builds a collection of labeled toggle switches. This
  13.  * useful when you want to create a set of exclusive or non-exclusive
  14.  * choices from a set of strings. <p>
  15.  * 
  16.  * The reason the 0th child is special is that all other children get
  17.  * added to his group. Groups are circularly linked lists of toggles
  18.  * who need to be exclusive of each other. The functions which manipulate
  19.  * "groups" with respect to toggles are defined in the toggle class.
  20.  *
  21.  * @see toggle
  22.  * @author Ian Smith
  23.  */
  24.  
  25. public class text_toggle_collection extends base_parent_interactor {
  26.  
  27.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  28.  
  29.   /**
  30.    * vector of the strings for this collection 
  31.    */
  32.   protected Vector _data;
  33.  
  34.   /**
  35.    * Access the vector of strings in this collection.
  36.    * @return Vector the strings in this collection.
  37.    */
  38.   public Vector data() { return _data;};
  39.  
  40.   /**
  41.    * Set the data in this collection to be (and its toggles to display)
  42.    * an array of strings.
  43.    * 
  44.    * @param String[] s the array of strings you want the user to choose from.
  45.    */
  46.   public void set_data(String[] s) {
  47.     int i;
  48.  
  49.     /* get rid of previous data */
  50.     clear_items();
  51.     for (i=0; i<s.length; ++i) {
  52.       set_item(i,s[i]);
  53.     }
  54.   }
  55.  
  56.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  57.  
  58.   /** 
  59.    * The callback object to send callbacks to.
  60.    */
  61.   protected callback_object _callback_obj;
  62.  
  63.   /**
  64.    * Access the object we send callbacks to. 
  65.    * 
  66.    * @return callback_object the object we send the callbacks to.
  67.    */
  68.   protected callback_object callback_obj() { return _callback_obj;};
  69.  
  70.   /**
  71.    * Modify the current callback object.
  72.    * 
  73.    * @param callback_object cb the new object to receive callbacks.
  74.    */
  75.   protected void set_callback_obj(callback_object cb ) { _callback_obj=cb;};
  76.  
  77.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  78.  
  79.   /** 
  80.    * Get rid of all items
  81.    */
  82.   public void clear_items() {
  83.     _data.removeAllElements();
  84.  
  85.       /* repeat this until no more children */
  86.     while (num_children()!=0) {
  87.       /* remove the first child */
  88.       remove_child(child(0));
  89.     }
  90.   }
  91.  
  92.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  93.  
  94.   /**
  95.    * Use group tells the collection if its collection forms one group or
  96.    * not.  It is only consulted when NEW items are added to the 
  97.    * collection. 
  98.    */
  99.   protected boolean _use_group;
  100.  
  101.   /**
  102.    * Is the collection using a group (exclusive choices)? 
  103.    * @return boolean true if this collection is a group. 
  104.    */
  105.   protected boolean use_group() { return _use_group;};
  106.  
  107.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  108.  
  109.   /**
  110.    * Change the state of the use_group variable. Note that it is only
  111.    * consulted as new items are added to the collection.
  112.    *
  113.    * @param boolean b whether or not this collection should use a group or 
  114.    *                  not (if this is true, then choices are exclusive).
  115.    */
  116.   protected void set_use_group(boolean b) { _use_group=b;};
  117.  
  118.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  119.  
  120.   /**
  121.    * This controls if you want a fixed width for the labels. If you specify
  122.    * a value other than negative 1, you get that width. It is only 
  123.    * considered when NEW items are added to the collection.
  124.    */
  125.   protected int _fixed_width=-1;
  126.  
  127.   /**
  128.    * Get the width of all items in this collection.
  129.    * 
  130.    * @return int the width all items in the collection or -1 if not 
  131.    * currently set.
  132.    */
  133.   protected int fixed_width() { return _fixed_width;};
  134.  
  135.   /**
  136.    * Set the value of the width of all items in the collection. Note that
  137.    * this value is only consulted when new items are added to the 
  138.    * collection. 
  139.    * @param int f the new fixed width for this collection (use -1 for no fixed 
  140.    *              width)
  141.    */
  142.   protected void set_fixed_width(int f) { _fixed_width=f;};
  143.  
  144.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  145.  
  146.   /**
  147.    * Make a toggle. Its useful to override this if you want a different
  148.    * toggle class.
  149.    *  
  150.    * @param int i which toggle (which possible) you want this new toggle to be 
  151.    * @return toggle the toggle we want added to the collection
  152.    */
  153.   protected toggle make_toggle(int i) 
  154. {
  155.     label_toggle t,tmp;
  156.  
  157.     if (use_group()) {
  158.       t=new label_toggle(true,callback_obj(), fixed_width(),font(),
  159.              (String)_data.elementAt(i));
  160.     } else {
  161.       t=new label_toggle(false,callback_obj(), fixed_width(),font(),
  162.              (String)_data.elementAt(i));
  163.     }
  164.  
  165.     t.set_user_info(new Integer(i));
  166.  
  167.     /* make a group? */
  168.     if (use_group()) {
  169.       /* we use #0 as the group leader, so he is not in a group */
  170.       if (i!=0) {
  171.     t.add_to_group_of(nth_toggle(0));
  172.       } 
  173.     }
  174.  
  175.     return t;
  176.   }
  177.  
  178.    //had:
  179.    //* @exception general PROPAGATED
  180.  
  181.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  182.  
  183.   /** 
  184.    * Set the ith item in the collection to display a certain string. 
  185.    * You better make sure the items are contiguous or you'll get a 
  186.    * null pointer exception when you try to redraw this object.<p> 
  187.    * 
  188.    * Items begin numbering at zero.
  189.    *
  190.    * @param int    i the number of the item you want to change.
  191.    * @param String s the new string to display.
  192.    */
  193.   public void set_item(int i,String s) {
  194.     label_toggle t;
  195.     /* put in the data list */
  196.     if (_data.size()==i) {
  197.  
  198.       // simple case is add at end
  199.       _data.addElement(s);
  200.  
  201.       /* make the object */
  202.       t=(label_toggle)make_toggle(i);
  203.  
  204.       /* add child */
  205.       add_child(t);
  206.  
  207.     } else {
  208.  
  209.       // better hope its bigger
  210.       _data.setElementAt(s,i);
  211.       t=(label_toggle) child(i);
  212.  
  213.     /* set the text to be right */
  214.       t.set_text(s);
  215.     }
  216.  
  217.     /* snap on constraints */
  218.     set_toggle_constraints(t,i);
  219.   }
  220.  
  221.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  222.  
  223.   /**
  224.    * Put the constraints on for the the toggle. 
  225.    * 
  226.    * @param toggle t the toggle we are constraining.
  227.    * @param int    i the position in the toggle of this collection.
  228.    */
  229.   protected void set_toggle_constraints(toggle t,int i) 
  230. {
  231.  
  232.     /* if first obj, then put it off the parent */
  233.     if (i==0) {
  234.       t.set_y_constraint(std_function.offset(PARENT.Y(), default_offset()));
  235.     } else {
  236.       /* space from previous child */
  237.       t.set_y_constraint(std_function.offset(PREV_SIBLING.Y2(), default_offset()));
  238.     }
  239.  
  240.     /* y is zero plus the offset in all cases */
  241.     t.set_x_constraint(std_function.offset(PARENT.X(), default_offset()));
  242.  
  243.     /* these things are sized internally on w and h */
  244.   }
  245.  
  246.    //had:
  247.    //* @exception general PROPAGATED.
  248.  
  249.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  250.  
  251.   /**
  252.    *  How much space around the edges and between the items.
  253.    */
  254.   protected static int _default_offset=2;
  255.  
  256.   /**
  257.    * Return the default distance around the edges of items.
  258.    * @return int the amount of space around items
  259.    */
  260.    protected static int default_offset() { return _default_offset;};
  261.  
  262.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  263.  
  264.   /**
  265.    * Build a new text/toggle collection object. This assumes that you
  266.    * will position this object with constraints.  If you want the objects
  267.    * formed into a group, set use_a_group. If you want all the labels to 
  268.    * be the same width, set fixed_width to the width; if you don't want
  269.    * fixed with, pass -1. The callback object is called when an object
  270.    * get selected. You'll get a user info value which has an Integer in
  271.    * it. That is the index number of the object just selected. 
  272.    *
  273.    * @param String[]        s           the initial set of strings to display.
  274.    * @param boolean         use_a_group pass true here if you want the set of 
  275.    *                    toggles to be exclusive, pass false if 
  276.    *                                    you want them to be non-exclusive.
  277.    * @param int             fw          the 'forced width' of the objects in 
  278.    *                                    the collection in pixels (if you use 
  279.    *                                    -1 the objects don't get forced to 
  280.    *                                    particular width).
  281.    * @param callback_object cb          the object you want callbacks 
  282.    *                                    delivered to.
  283.    */
  284.   public text_toggle_collection(String[] s, boolean use_a_group,
  285.                 int fw, callback_object cb) 
  286. {
  287.     super(0,0);
  288.  
  289.     _data=new Vector(10);
  290.     set_use_group(use_a_group);
  291.     set_fixed_width(fw);
  292.     set_callback_obj(cb);
  293.     set_data(s);
  294.  
  295.     /* just make us get around our children */
  296.     set_w_constraint(std_function.offset(MAX_CHILD.X2(), default_offset()));
  297.     set_h_constraint(std_function.offset(MAX_CHILD.Y2(), default_offset()));
  298.   }
  299.  
  300.    //had
  301.    //* @exception general PROPAGATED
  302.  
  303.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  304.  
  305.   /** 
  306.    * Indexing from 0, get the toggle associated with that index.
  307.    * @param int i the index in question
  308.    * @return toggle the interactor (toggle) for a particular index
  309.    */
  310.   public toggle nth_toggle(int n) 
  311. {
  312.     return (toggle)child(n);
  313.   }
  314.    //had
  315.    //* @exception general PROPAGATED
  316.  
  317.   /** 
  318.    * Get the String value for that an index.
  319.    * @param int i the index in question
  320.    * @return String the string associated with the index
  321.    */
  322.   public String nth_string(int n) {
  323.     return (String)_data.elementAt(n);
  324.   }
  325.  
  326.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  327.  
  328.   /**
  329.    * This is where the font lives for drawing the labels.
  330.    */
  331.   protected Font _font;
  332.  
  333.   /**
  334.    * Return the font we are drawing labels in. 
  335.    * @return Font the font for the strings displayed in this object.
  336.    */
  337.   public Font font() { return _font;}
  338.  
  339.   /**
  340.    * Set the font to use for this collection.
  341.    * @param Font f the new font to draw the labels in 
  342.    */
  343.   public void set_font(Font f) { 
  344.     String d[];
  345.     int i;
  346.  
  347.     _font=f;
  348.     d=new String[_data.size()];
  349.     for(i=0; i<_data.size(); ++i) {
  350.       d[i]=nth_string(i);
  351.     }
  352.     set_data(d);
  353.   }
  354.  
  355.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  356. }
  357. /*=========================== COPYRIGHT NOTICE ===========================
  358.  
  359. This file is part of the subArctic user interface toolkit.
  360.  
  361. Copyright (c) 1996 Scott Hudson and Ian Smith
  362. All rights reserved.
  363.  
  364. The subArctic system is freely available for most uses under the terms
  365. and conditions described in 
  366.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  367. and appearing in full in the lib/interactor.java source file.
  368.  
  369. The current release and additional information about this software can be 
  370. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  371.  
  372. ========================================================================*/
  373.